/**
 * Copyright 2009-2010 - YangJiandong(chunquedong)
 * 
 * This file is part of ChunMap project
 * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE(Version >=3)
 * 你可以自由复制、传播本项目的下载包文件，但必须保持其完整性。
 * 我们不用对使用中的风险及由此造成的损失承担任何责任。
 * 详细情况请见《ChunMap许可协议》。

 * 想了解更多有关ChunMap的信息，请访问http://code.google.com/p/chunmap/
 */
package chunmap.model.elem;

import chunmap.model.coord.Position;
import chunmap.model.coord.Coordinate2D;

/**
 * @author chunquedong
 * 
 */
public class Triangle {

	private final Position p1;
	private final Position p2;
	private final Position p3;

	/**
	 * @param p1
	 * @param p2
	 * @param p3
	 */
	public Triangle(Position p1, Position p2, Position p3) {
		super();
		this.p1 = p1;
		this.p2 = p2;
		this.p3 = p3;
	}

	public Position getP1() {
		return p1;
	}

	public Position getP2() {
		return p2;
	}

	public Position getP3() {
		return p3;
	}

	/**
	 * 三角形代数面积
	 * 
	 * @return
	 */
	public double computeArea() {
		Vector v1 = new Vector(p1, p2);
		Vector v2 = new Vector(p1, p3);
		double a = v1.get2DCrossProduct(v2);
		return a / 2d;
	}

	/**
	 * 外接圆圆心,垂直平分线法
	 */
	public Position computeOuterCenter() {
		Line l1 = getLine(p1, p2);
		Line l2 = getLine(p2, p3);

		Position point = l1.crossPoint(l2);
		return point;
	}

	public Position computeCenter() {
		double x = (p1.getX() + p2.getX() + p3.getX()) / 3d;
		double y = (p1.getY() + p2.getY() + p3.getY()) / 3d;

		return new Coordinate2D(x, y);
	}

	private Line getLine(Position p1, Position p2) {
		LineSegment s1 = new LineSegment(p1, p2);
		Line l1 = s1.toLine();
		double k1 = l1.getVerticalK();
		Position p = s1.getMiddlePoint();
		Line vl1 = new Line(p, k1);

		return vl1;
	}
}
